﻿#ifndef VDATA_MODBUS_MASTER_H
#define VDATA_MODBUS_MASTER_H

/*-----------------------------------头文件------------------------------------*/
#include "includes.h"

typedef struct {
    union{
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
        };
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint16_t start_addr;
            uint16_t coil_num;
            uint16_t crc16;
        }func_01_send;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint16_t start_addr;
            uint16_t input_status_num;
            uint16_t crc16;
        }func_02_send;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint16_t start_addr;
            uint16_t hold_reg_num;
            uint16_t crc16;
        }func_03_send;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint16_t start_addr;
            uint16_t input_reg_num;
            uint16_t crc16;
        }func_04_send;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint16_t start_addr;
            uint16_t coil_value;
            uint16_t crc16;
        }func_05_send;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint16_t start_addr;
            uint16_t hold_reg_value;
            uint16_t crc16;
        }func_06_send;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint16_t start_addr;
            uint16_t coil_num;
            uint8_t  bytes;
            uint8_t  *data;
            uint16_t crc16;
        }func_15_send;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint16_t start_addr;
            uint16_t hold_reg_num;
            uint8_t  bytes;
            uint16_t  *data;
            uint16_t crc16;
        }func_16_send;
    };
    union{
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint8_t  bytes;
            uint8_t  *coil_data;
            uint16_t crc16;
        }func_01_recv;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint8_t  bytes;
            uint8_t  *input_status_data;
            uint16_t crc16;
        }func_02_recv;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint8_t  bytes;
            uint16_t  *hold_reg_data;
            uint16_t crc16;
        }func_03_recv;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint8_t  bytes;
            uint16_t  *input_reg_data;
            uint16_t crc16;
        }func_04_recv;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint16_t coil_addr;
            uint16_t coil_data;
            uint16_t crc16;
        }func_05_recv;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint16_t hold_reg_addr;
            uint16_t hold_reg_data;
            uint16_t crc16;
        }func_06_recv;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint16_t start_addr;
            uint16_t coil_num;
            uint16_t crc16;
        }func_15_recv;
        struct {
            uint8_t  dev_addr;
            uint8_t  func_code;
            uint16_t start_addr;
            uint16_t reg_num;
            uint16_t crc16;
        }func_16_recv;
    };
    uint8_t err_code;
    uint8_t recv_length;
    uint8_t send_length;
    uint8_t recv_buff[520];
    uint8_t send_buff[520];
}vdata_modbus_master_t;

extern vdata_modbus_master_t vdata_modbus_master;

uint16_t modbus_master_crc16(uint8_t *puchMsg, uint16_t usDataLen);
uint16_t vdata_modbus_master_parse(vdata_modbus_master_t *pdata, uint8_t *raw_data, uint16_t length);
uint16_t vdata_modbus_master_packet(vdata_modbus_master_t *pdata);
uint16_t vdata_modbus_send_fun01(vdata_modbus_master_t *pdata, uint16_t start_addr, uint16_t coil_num);
uint16_t vdata_modbus_send_fun02(vdata_modbus_master_t *pdata, uint16_t start_addr, uint16_t input_status_num);
uint16_t vdata_modbus_send_fun03(vdata_modbus_master_t *pdata, uint16_t start_addr, uint16_t hold_reg_num);
uint16_t vdata_modbus_send_fun04(vdata_modbus_master_t *pdata, uint16_t start_addr, uint16_t input_reg_num);
uint16_t vdata_modbus_send_fun05(vdata_modbus_master_t *pdata, uint16_t start_addr, uint16_t coil_value);
uint16_t vdata_modbus_send_fun06(vdata_modbus_master_t *pdata, uint16_t start_addr, uint16_t hold_reg_value);
uint16_t vdata_modbus_send_fun15(vdata_modbus_master_t *pdata, uint16_t start_addr, uint16_t coil_num, uint8_t *data);
uint16_t vdata_modbus_send_fun16(vdata_modbus_master_t *pdata, uint16_t start_addr, uint16_t hold_reg_num, uint16_t *data);

void vdata_modbus_master_sem_post(void *sem);
void vdata_modbus_master_data_out(uint8_t *buff, uint16_t length);
void vdata_set_modbus_master_addr(uint8_t master_addr);
#endif

